package 力扣日常刷题.木22二月.第14天220;

/**
 * @author 帅小伙
 * @date 2022/2/20
 * @description
 */
public class Demo106接雨水 {

    public int trap(int[] height) {
        int ans = 0;
        int left = 0 , right = height.length - 1;
        int leftMax = 0,rightMax = 0;
        while (left < right) {
            leftMax = Math.max(leftMax,height[left]);
            rightMax = Math.max(rightMax,height[right]);
            if(leftMax < rightMax) {
                ans += (leftMax - height[left]);
                ++left;
            }else {
                ans += (rightMax - height[right]);
                --right;
            }
        }
        return ans;
    }

}
